package acme.bloodtime.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.api.DataObject;

/**
 * 
 * @author Manuel A. Quintero V.
 * 
 */

public abstract class ArticuloAbstractDAO extends ArticuloDAO {

	public DataObject loadById(int id) throws SQLException {
		StringBuffer strbuf = new StringBuffer();
		ArticuloDAO articuloDAO = new ArticuloDAO();
		strbuf.append("SELECT * FROM ");
		strbuf.append(getTableName() + "," + articuloDAO.getTableName());

		strbuf.append(" WHERE ");
		strbuf.append(articuloDAO.getTableName() + DataObject.ID);
		strbuf.append(" = ");
		strbuf.append(ArticuloDO.ID);
		strbuf.append(" and ");
		strbuf.append(articuloDAO.getTableName() + DataObject.ID);
		strbuf.append(" = ");
		strbuf.append(id);

		System.err.println(strbuf.toString());

		ResultSet rs = //
		connection.createStatement().executeQuery(strbuf.toString());

		if (rs.next()) {
			return resultSetToDO(rs);
		}

		return null;
	}

	@Override
	public List<DataObject> listAll(int lim, int off) throws SQLException {
		StringBuffer strbuf = new StringBuffer();

		ArticuloDAO articuloDAO = new ArticuloDAO();

		strbuf.append("SELECT * FROM ");
		strbuf.append(articuloDAO.getTableName() + "," + getTableName());
		strbuf.append(" WHERE  " + ArticuloDO.ID + "="
				+ articuloDAO.getTableName() + ArticuloDO.ID);

		if (lim >= 0 && off >= 0) {
			strbuf.append(" LIMIT  ");
			strbuf.append(lim);
			strbuf.append(" OFFSET ");
			strbuf.append(off);
		}

		System.err.println(strbuf.toString());

		ResultSet rs = //
		connection.createStatement().executeQuery(strbuf.toString());

		List<DataObject> ret = new ArrayList<DataObject>();

		while (rs.next()) {
			ret.add(resultSetToDO(rs));
		}
		return ret;
	}
	

}
